package com.jwserver;

import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@RestController
public class jwGetMethod {
    @RequestMapping(value = "/getCookies222", method = RequestMethod.GET)
    @ApiOperation(value = "通过这个方法可以获取到Cookies",httpMethod = "GET")
    public String getCookies(HttpServletResponse response) {
        //HttpServerletRequest 装请求信息的类
        //HttpServerletResponse  装响应信息的类
        Cookie cookie = new Cookie("login", "11true");
        response.addCookie(cookie);
        return "恭喜你获得cookies信息成功";


    }

    @RequestMapping(value = "/jwget/with/cookies", method = RequestMethod.GET)
    @ApiOperation(value = "要求客户端携带cookies访问",httpMethod = "GET")
    public String getWithCookies(HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        if (Objects.isNull(cookies)) {
            return "你必须携带cookies信息来";
        }
        for (Cookie cookie : cookies) {
            if (cookie.getName().equals("login") &&
                    cookie.getValue().equals("true")) {
                return "这是一个需要携带cookies信息才能访问的get请求!";
            }
        }

        return "你必须携带cookies信息来";
    }


    @RequestMapping(value = "/get/with/param", method = RequestMethod.GET)
    @ApiOperation(value = "需求携带参数才能访问的get请求的第一种实现",httpMethod = "GET")
    public Map<String, Integer> getList(@RequestParam Integer start,
                                        @RequestParam Integer end) {
        Map<String, Integer> myList = new HashMap<>();

        myList.put("鞋", start);
        myList.put("干脆面", end);
        myList.put("衬衫", 300);

        return myList;
    }


    @RequestMapping(value = "/get/with/param/{start}/{end}")
    @ApiOperation(value = "需求携带参数才能访问的get请求的第二种实现",httpMethod = "GET")
    public Map myGetList(@PathVariable Integer start,
                         @PathVariable Integer end) {

        Map<String, Integer> myList = new HashMap<>();

        myList.put("鞋", start);
        myList.put("干脆面", end);
        myList.put("衬衫", 300);

        return myList;

    }

}